package io.github.definitlyevil.ziminmod.listeners;

import de.slikey.effectlib.effect.CubeEffect;
import io.github.definitlyevil.customitemframework.CIFItemInstance;
import io.github.definitlyevil.customitemframework.CustomItemFramework;
import io.github.definitlyevil.ziminmod.ZiMinMod;
import io.github.definitlyevil.ziminmod.cif.tools.ZiMinPickaxe;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;

import java.util.Random;

public class ZiMinPickaxeListener implements Listener {

    private final Random random = new Random();

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onBlockBreak(BlockBreakEvent event) {
        Player player = event.getPlayer();
        ItemStack item = player.getInventory().getItemInMainHand();
        CIFItemInstance instance = CustomItemFramework.getCIFDataFromItem(item);
        if(instance == null || !ZiMinPickaxe.class.isAssignableFrom(instance.type.getClass())) return;
        CubeEffect effect = new CubeEffect(ZiMinMod.getInstance().getEffectManager());
        Location location = event.getBlock().getLocation();
        location.setX(location.getBlockX() + .5d);
        location.setY(location.getBlockY() + .5d);
        location.setZ(location.getBlockZ() + .5d);
        effect.setLocation(location);
        effect.edgeLength = 1;
        effect.particles = 32;
        effect.particle = Particle.WATER_DROP;
        effect.duration = 10;
        effect.start();

        location.getWorld().playSound(location, "zimin.effect.swoosh", 1.0f,
            1.0f + ((random.nextBoolean() ? 1f : -1f) * random.nextFloat() * 0.2f));
    }

}
